package main

func romanToInt(s string) int {
	m := map[int]int{
		'I': 1,
		'V': 5,
		'X': 10,
		'L': 50,
		'C': 100,
		'D': 500,
		'M': 1000,
	}
	r := 0
	var tv int
	var n uint8
	maxI := len(s) - 1
	for i, c := range s {
		tv = m[int(c)]
		if i < maxI {
			n = s[i+1]
			switch c {
			case 'I':
				if n == 'V' || n == 'X' {
					r -= tv
					continue
				}
			case 'X':
				if n == 'L' || n == 'C' {
					r -= tv
					continue
				}
			case 'C':
				if n == 'D' || n == 'M' {
					r -= tv
					continue
				}
			}
		}
		r += tv
	}

	return r
}

func main() {
	println(romanToInt("III"))
	println(romanToInt("IV"))
	println(romanToInt("IX"))
	println(romanToInt("MCMXCIV"))
}
